import 'dart:async';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:short_video_app/ui/pages/publish/publish_page.dart';
import 'package:short_video_app/ui/widgets/custom_scaffold.dart';
import 'package:short_video_app/view_model/publish/camera_view_model.dart';
import 'package:video_player/video_player.dart';

import '../../../utils/asset_util.dart';
import '../../../utils/color_util.dart';
import '../../../utils/navigator_util.dart';
import '../../widgets/video_view.dart';

class CameraPage extends StatefulWidget {
  const CameraPage({Key? key}) : super(key: key);

  @override
  State<CameraPage> createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
  var _image;
  var imagePicker;
  var type;
  var _video;

  @override
  void initState() {
    // ignore: todo
    // TODO: implement initState
    super.initState();
    imagePicker = new ImagePicker();
  }

  @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //     body: Center(
  //       child: GestureDetector(
  //         onTap: () async {
  //           XFile image = await imagePicker.pickImage(
  //               source: ImageSource.camera,
  //               imageQuality: 50,
  //               preferredCameraDevice: CameraDevice.rear);
  //           setState(() {
  //             _image = File(image.path);
  //             _video=null;
  //             debugPrint("-----${_image}-----");
  //           });
  //         },
  //         onLongPress: () async {
  //           XFile image = await imagePicker.pickVideo(
  //               source: ImageSource.camera,
  //               // maxDuration: const Duration(hours: 0, minutes: 0, seconds: 15),
  //               preferredCameraDevice: CameraDevice.rear);
  //           setState(() {
  //             _video = File(image.path);
  //             _image=null;
  //             debugPrint("-----${_video}-----long");
  //           });
  //         },
  //         child: Stack(
  //           alignment: AlignmentDirectional.bottomStart,
  //           fit: StackFit.expand,
  //           // decoration: BoxDecoration(color: Colors.red[200]),
  //           children: <Widget>[
  //             _image != null || _video != null
  //                 ? _image != null
  //                     ? Image.file(
  //                         _image,
  //                         fit: BoxFit.fitHeight,
  //                       )
  //                     : VideoView(url: _video.toString())
  //                 : Image.network(
  //                     "https://ts4.cn.mm.bing.net/th?id=ODL.eaed39534c02395697b2e1021790ef3f&w=146&h=146&c=7&rs=1&qlt=80&o=6&pid=RichNav",
  //                     fit: BoxFit.fitHeight,
  //                   ),
  //             const Positioned(
  //               // decoration: BoxDecoration(color: Colors.red[200]),
  //               bottom: 10,
  //               left: 150,
  //               child: Icon(
  //                 Icons.camera_alt,
  //                 size: 50,
  //                 color: ColorsUtil.COLOR_FFFFFF,
  //               ),
  //             ),
  //             Positioned(
  //                 // decoration: BoxDecoration(color: Colors.red[200]),
  //                 bottom: 10,
  //                 left: 220,
  //                 child: GestureDetector(
  //                   child: AssetUtil.svgImage("publish/right",
  //                       size: const Size(50, 50),
  //                       color: ColorsUtil.COLOR_FFFFFF),
  //                   behavior: HitTestBehavior.opaque,
  //                   onTap: () {
  //                     NavigatorUtil.push(context, const PublishPage());
  //                     // viewModel.publish(context);
  //                   },
  //                 )),
  //             Positioned(
  //                 // decoration: BoxDecoration(color: Colors.red[200]),
  //                 bottom: 15,
  //                 left: 80,
  //                 child: GestureDetector(
  //                   child: AssetUtil.svgImage("publish/error",
  //                       size: const Size(40, 40),
  //                       color: ColorsUtil.COLOR_FFFFFF),
  //                   behavior: HitTestBehavior.opaque,
  //                   onTap: () {
  //
  //                     // viewModel.publish(context);
  //                   },
  //                 )),
  //           ],
  //         ),
  //       ),
  //     ),
  //   );
  // }

  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => CameraViewModel(imagePicker),
      child: Consumer<CameraViewModel>(builder: (context, viewModel, child) {
        return _buildContent(viewModel);
      }),
    );
  }

  Widget _buildContent(viewModel) {
    return Center(
      child: Stack(
        alignment: AlignmentDirectional.bottomStart,
        fit: StackFit.expand,
        // decoration: BoxDecoration(color: Colors.red[200]),
        children: <Widget>[
          _image != null || _video != null
              ? _image != null
                  ? Image.file(
                      _image,
                      fit: BoxFit.fitHeight,
                    )
                  : VideoView(url: _video.toString())
              : Image.network(
                  "https://ts4.cn.mm.bing.net/th?id=ODL.eaed39534c02395697b2e1021790ef3f&w=146&h=146&c=7&rs=1&qlt=80&o=6&pid=RichNav",
                  // _video
                  fit: BoxFit.fitHeight,
                ),
          Positioned(
            bottom: 35,
            left: 150,
            child: GestureDetector(
              child: const Icon(
                Icons.camera_alt,
                size: 50,
                color: ColorsUtil.COLOR_FFFFFF,
              ),
              onTap: () async {
                await viewModel.setImage();
                setState(() {
                  _image = File(viewModel.image.path);
                  _video=null;
                  // debugPrint("-----${_image}----|${_video}-");
                });
              },
              onLongPress: () async {
                await viewModel.setVideo();
                setState(() {
                  _video = File(viewModel.video.path).uri;
                  _image =null;
                  // debugPrint("-----${_video}-----long | ---${_image}--");
                });
              },
            ),
          ),
          const Positioned(
              bottom: 10,
              left: 115,
              child: Text(
                "单击拍照,长按拍摄",
                style: TextStyle(
                  color: ColorsUtil.COLOR_FFFFFF,
                ),
              )),
          Positioned(
              bottom: 35,
              left: 220,
              child: GestureDetector(
                child: AssetUtil.svgImage("publish/right",
                    size: const Size(50, 50), color: ColorsUtil.COLOR_FFFFFF),
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  viewModel.gotoPushPage(context);
                },
              )),
          Positioned(
              bottom: 40,
              left: 80,
              child: GestureDetector(
                child: AssetUtil.svgImage("publish/error",
                    size: const Size(40, 40), color: ColorsUtil.COLOR_FFFFFF),
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  viewModel.close(context);
                },
              )),
          Positioned(
              bottom: 60,
              left: 300,
              child: GestureDetector(
                child: AssetUtil.svgImage("publish/picture",
                    size: const Size(40, 40), color: ColorsUtil.COLOR_666666),
                behavior: HitTestBehavior.opaque,
                onTap: () async {
                 await viewModel.select();
                 setState(() {
                   _image = File(viewModel.image.path);
                   debugPrint("-----${_image}-----");
                 });
                },
              )),
        ],
      ),
    );
  }
}
